package com.bianguo.zsd.SpringBootDemo.config;

import com.bianguo.zsd.SpringBootDemo.bean.BaseEntity;
import com.bianguo.zsd.SpringBootDemo.bean.User;
import com.bianguo.zsd.SpringBootDemo.repository.UserRepository;
import com.bianguo.zsd.SpringBootDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

import java.util.Collection;
import java.util.Optional;

//标注在类上，相当于把该类作为spring的xml配置文件中的<beans>，作用为：配置spring容器(应用上下文)
@Configuration
public class UserConfig {

    //可理解为用spring的时候xml里面的<bean>标签。
    @Bean
    @Autowired
    public RouterFunction<ServerResponse> personFindAll(UserRepository userRepository) {
        return RouterFunctions.route(RequestPredicates.GET("/person/find/all"),
                serverRequest -> {
                    BaseEntity<Collection<User>> baseEntity = new BaseEntity<>();
                    baseEntity.setStatus(200);
                    baseEntity.setMsg("获取成功");
                    Collection<User> users = userRepository.findAll();
                    baseEntity.setData(users);
//                    Flux<BaseEntity> flux = Flux.just(baseEntity);
//                    Mono<ServerResponse> mono = ServerResponse.ok().body(flux, BaseEntity.class);
                    Mono<ServerResponse> mono = ServerResponse.ok().body(BodyInserters.fromObject(baseEntity));
                    return mono;
                });
    }
}
